<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Controllers\Controller;

class TypeController extends Controller{
    public function getIndex(Request $request){
        $data = DB::table('sa_type')
            -> paginate(10);
        //dd($data);
        return view('admin.type.index',['data' => $data,'request'=>$request->all()]);
    }

    //添加设备类型页面
    public function getCreate()
    {
        return view('admin.type.create');
    }

    //添加设备类型处理页面
    public function postStore(Request $request)
    {
        $data = array();
        $input = $request->except('_token');
        $data['type'] = $input['type'];
        $data['app_eui'] = $input['AppEUI'];
        @$insert = DB::table('sa_type') ->insert($data);
        if($insert){
            return redirect('admin/type/index')->with('success','添加成功');
        }else{
            return back() ->withInput() -> with('error','添加失败');
        }
    }

    //编辑设备类型页面
    public function getEdit(Request $request)
    {
        $id = $request -> get('id');
        $info = DB::table('sa_type')
            -> where('id','=',$id)
            -> first();
        // dd($info);
        return view('admin.type.edit',['info'=>$info]);
    }

    //编辑设备类型处理页面
    public function postUpdate(Request $request)
    {
        $data = array();
        $id = $request -> only('id');
        $input = $request -> except('_token','id');
        $data['type'] = $input['type'];
        $data['app_eui'] = $input['AppEUI'];
        $update = DB::table('sa_type') -> where('id','=',$id) -> update($data);
        if($update){
            return redirect('admin/type/index')->with('success','修改成功');
        }else{
            return back() ->withInput() -> with('error','修改失败');
        }
    }
}
